package request

import (
	"Squid/enum/fileds"
	"gorm.io/gorm"
)

// Paging  分页参数
type Paging struct {
	Page      int  `json:"page"`
	Size      int  `json:"size"`
	Sort      string `json:"sort"`
	SortFiled string `json:"sort_filed"`
}

// ValidateSort 检验sort字段正序还是倒序
func ValidateSort(sort string) bool {
	var validFiled = []string{fileds.ASC, fileds.DESC}
	for _, v := range validFiled{
		if v == sort {
			return true
		}
	}
	return false
}

// Paginate 分页
func Paginate(offset int, size int) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Offset(offset).Limit(size)
	}
}

// FilterLikeWhere 通用scope拼接查询
func FilterLikeWhere(field string, query string) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		if query == "" {
			return db
		} else {
			return db.Where(field+" LIKE ?", "%"+query+"%")
		}
	}
}
